package com.yc.mybatis.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


public class DBUtil {
	/**
	 * 更新操作
	 * @param con
	 * @param sql
	 * @param params
	 * @return
	 */
	public int update(Connection con, String sql, List<Object> params) {
		if(params == null || params.isEmpty()) {
			return this.update(con, sql);
		}
		return this.update(con, sql, params.toArray());
	}

	public int update(Connection con, String sql,Object ...params) {
		int result = 0;
		PreparedStatement pstmt = null;
		
		try {
			pstmt = con.prepareStatement(sql);//预编译
			setParams(pstmt,params);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.close(null,pstmt);
		}
		return result;
	}
/**
 * 关闭资源
 * @param rs
 * @param pstmt
 */
	private void close(ResultSet rs, PreparedStatement pstmt) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if(pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
/**
 * 给占位符赋值
 * @param psmt
 * @param params
 */
	private void setParams(PreparedStatement psmt, Object ... params) {
		if(params == null || params.length<0) {
			return;
		}
		
		for(int i=0,len = params.length; i<len;++i) {
			try {
				psmt.setObject(i + 1, params[i]);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
/**
 * 返回多行数据的map对象
 * @param con
 * @param sql
 * @param paramsValue
 * @return
 */
public <T>List<T> findList(Connection con,Class<?> c, String sql, List<Object> params) {
	if(params == null || params.size() <= 0) {
		return this.findList(con, c , sql);
	}
	return this.findList(con, c, sql, params.toArray());
}

public <T>List<T> findList(Connection con,Class<?> c, String sql, Object ... params) {
	PreparedStatement pstmt = null;
	ResultSet rs= null;
	List<T> result = new ArrayList<T>();
	
	try {
		pstmt = con.prepareStatement(sql);
		this.setParams(pstmt, params);
		rs = pstmt.executeQuery();
		T t = null;
		while (rs.next()) {
			
		}
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		this.close(rs, pstmt);
	}
	return result;
}


	public List<Map<String,Object>> findList(Connection con, String sql, List<Object> params){
		if(params == null || params.size() <= 0) {
			return this.findList(con, sql);
		}
		return this.findList(con, sql, params.toArray());
	}

	private List<Map<String, Object>> findList(Connection con, String sql, Object ... array) {
		List<Map<String,Object>> list =new ArrayList<Map<String, Object>>();
		return list;
	}

	public Integer total(Connection con, String sql, List<Object> params) {
		if(params == null || params.size() <=0) {
			return this.total(con, sql);
		}
		return null;
	}

	private Integer total(Connection con, String sql,Object ...params) {
		int result = 0;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			pstmt = con.prepareStatement(sql); //预编译执行语句
			this.setParams(pstmt, params);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				result = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.close(rs, pstmt);
		}
		return result;
	}

	public Map<String,Object> findOne(Connection con, String sql, List<Object> parmas) {
		if(parmas == null || parmas.size() <=0) {
			return this.findOne(con, sql);
		}
		return this.findOne(con, sql, parmas.toArray());
	}

	private Map<String, Object> findOne(Connection con, String sql,Object ...params) {
		List<Map<String,Object>> result =this.findList(con, sql, params);
		if(result == null || result.isEmpty()) {
			return Collections.emptyMap();
		}
		return result.get(0);
	}
}
